package com.concurrent.demo;

import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Allocator {
    ReentrantLock lock = new ReentrantLock();


    private List<Object> als;
    // 一次性申请所有资源
    synchronized void apply(Object from,Object to){
        while(als.contains(from) || als.contains(to)){
            try {
                wait();
            } catch (Exception e) {

            }
        }
        als.add(from);
        als.add(to);
    }

    synchronized void free(Object from,Object to){
        als.remove(from);
        als.remove(to);
        notifyAll();
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("计算结果："+sum(10));
            }
            public long sum(int i){
                long sum = 0;
                while(i<100000){
                    sum +=i;
                    i++;
                }
                return sum;
            }
        });
        thread.start();
        Integer a = new Integer(3);
        Integer b = new Integer(3);
        System.out.println(a==b);//输出什么？
        Integer c = 3;//自动装箱，会调用Integer.valueOf方法，此方法会判断装箱的值是否在缓存范围内，如果在，则直接返回缓存的对象
        Integer d = 3;
        System.out.println(c==d);//输出什么？？
    }
}